# author: mikeqiu95
# date: 2022-8-21 15:46
# 说明: 创建允许进行多选的列表框控件
# IDE: Visual Studio Code
# Software Version: 1.0
# PowerShell: 5.1
# 帮助文档: https://docs.microsoft.com/zh-cn/powershell/scripting/samples/multiple-selection-list-boxes?view=powershell-7.2

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

# 创建空白窗体，并保存在$form变量
$form = New-Object System.Windows.Forms.Form
# 窗体标题
$form.Text = 'Data Entry Form'
# 绘制窗体大小
$form.Size = New-Object System.Drawing.Size(300,200)
# 设置窗体定位
$form.StartPosition = 'CenterScreen'

# 创建按钮控件，并保存在$okButton变量
$okButton = New-Object System.Windows.Forms.Button
# 设置按钮控件在窗体的位置，左上为(0,0)然后一直到右下(n,m)
$okButton.Location = New-Object System.Drawing.Point(75,120)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please make a selection from the list below.'
$form.Controls.Add($label)

$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10,40)
$listBox.Size = New-Object System.Drawing.Size(260,20)

# 这个就是同时可以选择多个值，否则就鼠标滑下去的时候都只能一个个点
$listBox.SelectionMode = 'MultiExtended'

[void] $listBox.Items.Add('Item 1')
[void] $listBox.Items.Add('Item 2')
[void] $listBox.Items.Add('Item 3')
[void] $listBox.Items.Add('Item 4')
[void] $listBox.Items.Add('Item 5')

$listBox.Height = 70
$form.Controls.Add($listBox)
$form.TopMost = $true

$result = $form.ShowDialog()

if($result -eq [System.Windows.Forms.DialogResult]::OK){
    $x = $listBox.SelectedItems
    $x
}